﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyPattern.VirualDemo
{
    /// <summary>
    /// 代理類
    /// </summary>
    public class Proxy : Subject
    {
        /// <summary>
        /// 要代理那個實現類
        /// </summary>
        private Subject subject = null;

        /// <summary>
        /// 默認被代理者
        /// </summary>
        public Proxy()
        {
            this.subject = new Proxy();
        }

        /// <summary>
        /// 通過構造函數傳遞代理者
        /// </summary>
        /// <param name="obj"></param>
        public Proxy(object obj)
        {
            //...
        }

        /// <summary>
        /// 代理構造函數
        /// </summary>
        /// <param name="_subject"></param>
        public Proxy(Subject _subject)
        {
            this.subject = _subject;
        }

        /// <summary>
        /// 實現接口中定義的方法
        /// </summary>
        public void request()
        {
            if(subject == null)
            {
                subject = new RealSubject();
            }
            subject.request();
        }

        /// <summary>
        /// 預處理
        /// </summary>
        private void before()
        {
            //do something
        }

        /// <summary>
        /// 善後處理
        /// </summary>
        private void after()
        {
            //do something
        }
    }
}
